/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.sipper.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;

public class IOSimpleGraph {

	private static String tag = "(?:([0-9]+)\\s+([0-9]+)\\s+(.+))";
	//private static String tag = "(?:([0-9]+)\\s+([0-9]+)\\s+((?:[0-9]+(?:\\.[0-9]+)?)))";
	
	public static DefaultDirectedWeightedGraph<Integer, DefaultWeightedEdge> load(String fileIn) throws IOException
	{
		//System.err.println(fileIn);
		DefaultDirectedWeightedGraph<Integer, DefaultWeightedEdge> graph;
		graph = new DefaultDirectedWeightedGraph<Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
		BufferedReader in;
		in = new BufferedReader (new FileReader (fileIn));
		// ajout sommets
		String ch = in.readLine();
		Integer number = Integer.valueOf(ch);
		for (int i=0; i<number;i++)
		{
			graph.addVertex(new Integer(i));
		}

		//ajout arcs
		Matcher m;
		
		Pattern reco = Pattern.compile(tag);
		while ((ch = in.readLine()) != null)
		{
			try
			{
				m = reco.matcher(ch);
				while (m.find())
				{
					Integer src = Integer.valueOf(m.group(1));
					Integer dest = Integer.valueOf(m.group(2));
					Double poids = Double.valueOf(m.group(3));
					DefaultWeightedEdge e = graph.addEdge(src, dest);
					graph.setEdgeWeight(e, poids);
				}
			}
			catch (Exception e)
			{
				System.err.println("File content error! :\n"+ch);
				e.printStackTrace();
				System.exit(1);
			}
		}
		//System.err.println(graph);
		return graph;
	}
	

	public static Map<Integer,String> loadLabels(String fileIn) throws IOException
	{
		Map<Integer,String> result = new HashMap<Integer,String>();
		BufferedReader in;
		in = new BufferedReader (new FileReader (fileIn));
		Matcher m;
		Pattern reco = Pattern.compile("(?:^([0-9]+)\\s(\\S+))");
		String ch = null;
		while ((ch = in.readLine()) != null)
		{
			m = reco.matcher(ch);
			while (m.find())
			{
				result.put(Integer.parseInt(m.group(1)), m.group(2));
			}
		}
		in.close();
		return result;
	}

	public static void export(DefaultDirectedWeightedGraph<Integer, DefaultWeightedEdge> graph, String fileOut) throws IOException
	{
		BufferedWriter out;
		out = new BufferedWriter(new FileWriter(fileOut));
		out.write(graph.vertexSet().size() +"\n\n");
		for (DefaultWeightedEdge e : graph.edgeSet())
		{
			out.write(graph.getEdgeSource(e) + "\t" + graph.getEdgeTarget(e) + "\t" + graph.getEdgeWeight(e) + "\n");
		}
		out.close();
	}

	public static void exportLabels(Map<Integer, String> labels, String fileOut) throws IOException
	{
		BufferedWriter out;
		out = new BufferedWriter(new FileWriter(fileOut));
		
		for (Entry<Integer, String> entry : labels.entrySet())
		{
			out.write(entry.getKey() + "\t" + entry.getValue() + "\n");
		}
		out.close();
	}
	
	
	public static void main(String[] argv)
	{
		try {
			DefaultDirectedWeightedGraph<Integer, DefaultWeightedEdge> graph = IOSimpleGraph.load(argv[0]);
			System.out.println(graph);
			IOSimpleGraph.export(graph,argv[1]);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
